#include "global.h"

#include <stddef.h>

#include "sqlite3.h"

#include "tags.h"

#include "log/log.h"

#define SQLITE_LOG_VERBOSE true

static void sqliteLogCallback(void* data, int err, const char* msg) {
    bool verboseLog = !!data;
    int errType = err & 255;
    if (errType == 0 || errType == SQLITE_CONSTRAINT || errType == SQLITE_SCHEMA
            || errType == SQLITE_NOTICE || err == SQLITE_WARNING_AUTOINDEX) {
        if (verboseLog) {
        	log_print(ANDROID_LOG_VERBOSE, TAG_NIM_INDEX_SQLITE, "(%d) %s\n", err, msg);
        }
    } else if (errType == SQLITE_WARNING) {
        log_print(ANDROID_LOG_VERBOSE, TAG_NIM_INDEX_SQLITE, "(%d) %s\n", err, msg);
    } else {
        log_print(ANDROID_LOG_VERBOSE, TAG_NIM_INDEX_SQLITE, "(%d) %s\n", err, msg);
    }
}

void global_init() {
	sqlite3_config(SQLITE_CONFIG_MULTITHREAD);

    sqlite3_config(SQLITE_CONFIG_LOG, &sqliteLogCallback, SQLITE_LOG_VERBOSE ? (void*)1 : NULL);

	sqlite3_initialize();
}
